package com.example.myapplication.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.view.menu.MenuAdapter;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.activity.AddMessageActivity;
import com.example.myapplication.activity.UpdateMessageActivity;
import com.example.myapplication.adapter.MessageListAdapter;
import com.example.myapplication.dao.MessageDao;
import com.example.myapplication.entity.Message;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends Fragment {

    // View
    private ListView messageListView;
    private ImageView addMessageImageView;
    private MessageDao messageDao;

    private List<Message> messageList;
    private MessageListAdapter adapter;

    public MessageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        messageListView = view.findViewById(R.id.lv_message_list);
        addMessageImageView = view.findViewById(R.id.iv_addMessage);
        addMessageImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转AddMessageActivity
                Intent intent = new Intent(getActivity(), AddMessageActivity.class);
                startActivity(intent);
            }
        });
        messageDao = new MessageDao(getContext());
        showListView();
        registerForContextMenu(messageListView);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("MessageFragment", "更新数据集");
        messageList = messageDao.findAllMessages();
        adapter = new MessageListAdapter(getContext(), messageList);
        messageListView.setAdapter(adapter);
    }

    void showListView() {
        // 初始化一些数据
//        List<Message> messageList = new ArrayList<>();
//        messageList.add(new Message(null ,"闫科宇", "Hi", "昨天", "portrait"));
//        messageList.add(new Message(null, "用户A", "Hello", "下午3:11", "portrait2"));
//        messageList.add(new Message(null, "用户B", "Good morning", "中午12:21", "portrait3"));
//        messageList.add(new Message(null, "用户C", "6542753", "上午9:00","portrait4"));
        // 1. 读取所有Message
        messageList = messageDao.findAllMessages();
        // 2. 显示在ListView中
        adapter = new MessageListAdapter(getContext(), messageList);
        messageListView.setAdapter(adapter);
    }

    @Override
    public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(1, 1, 1, "标为已读");
        menu.add(1, 2, 2, "置顶聊天");
        menu.add(1, 3, 3, "删除该聊天");
        menu.add(1, 4, 4, "修改");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        Log.d("MessageFragment", "onContextItemSelectd 被选中");
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Log.d("MessageFragment", "menuInfo position: " + menuInfo.position);
        Log.d("MessageFragment", "item.getItemId(): " + item.getItemId());
        switch (item.getItemId()) {
            case 1: // 标为已读

                break;
            case 2: // 置顶聊天

                break;
            case 3: // 删除该聊天
                Log.d("MessageFragment", "delete position: " + menuInfo.position);
                Message message = messageList.get(menuInfo.position);
                messageDao.deleteMessageById(message.getId());
                messageList.remove(message);
                adapter.notifyDataSetChanged();
                break;
            case 4: // 修改
                Message message2 = messageList.get(menuInfo.position);
                Intent intent = new Intent(getActivity(), UpdateMessageActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("messageInfo", message2);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);

            default:
                return super.onContextItemSelected(item);
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("onActivityResult", "requestCode: " + requestCode);
        Log.i("onActivityResult", "resultCodeCode: " + resultCode);
        if (resultCode == 1) {
            messageList = messageDao.findAllMessages();
            adapter = new MessageListAdapter(getContext(), messageList);
            messageListView.setAdapter(adapter);
        }
    }
}
